package cn.audit.service.operation;

import cn.audit.bean.pojo.*;
import cn.audit.bean.dto.UserCorrelation;
import cn.audit.repository.operation.CheckitemRepository;
import cn.audit.repository.operation.ExtractionUnitRepository;
import cn.audit.repository.operation.ProjectsItemRepository;
import cn.audit.repository.operation.TaskocationRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author fei人类
 * @date 2019/05/20
 */
@Service
public class ProjectsItemService {
    @Resource
    private CheckitemRepository checkitemRepository;

    @Resource
    private ExtractionUnitRepository extractionUnitRepository;

    @Resource
    private ProjectsItemRepository projectsItemRepository;

    @Resource
    private TaskocationRepository taskocationRepository;

    public List<CheckItem> findCheckItemAll() {
        return checkitemRepository.findAll();
    }

    public List<ExtractionUnit> findUnitAll() {
        return extractionUnitRepository.findAll();
    }

    public List<Project1> findProject1All() {
        return projectsItemRepository.findAll();
    }

    public List<Project1> findAll() {
        return projectsItemRepository.findAll();
    }

    public List<UserCorrelation> findItemAll() {
        return projectsItemRepository.findItemAll();
    }

    public UserCorrelation findItemById(Long id) {
        return projectsItemRepository.findItemById(id);
    }

    public List<Taskocation> findTaskocationAll() {
        return taskocationRepository.findAll();
    }

    public List<UserCorrelation> findUserAll() {
        return projectsItemRepository.findUserAll();
    }


    public Project1 saveProject(Project1 project1) {
        return projectsItemRepository.save(project1);
    }

    /**
     * 项目中添加主审助审
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateProject(Long id, Long lord, Long adjudication) {
        projectsItemRepository.updateProject(id, lord, adjudication);
    }

    /**
     * 任务分配
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateItem(Long id, String taskId, Long userId) {
        projectsItemRepository.updateItem(id, taskId);
        List<Long> ids = new ArrayList<>();
        if (taskId != null && taskId != "") {
            String[] s = taskId.trim().split(",");
            for (int i = 0; i < s.length; i++) {
                ids.add(Long.parseLong(s[i]));
            }
        }
        for (int i = 0; i < ids.size(); i++) {
            System.out.println("id" + ids.get(i));
        }
        taskocationRepository.updateItem(ids, userId);
    }

    /**
     * 计划项目进度
     *
     * @return
     */
    public List<UserCorrelation> findProjectProgress() {
        return projectsItemRepository.findProjectProgress();
    }


    /**
     * 人员任务总览
     *
     * @return
     */
    public Page<UserCorrelation> findProject2(Pageable page) {
        return projectsItemRepository.findProject2(page);
    }


    /**
     * 领导工作台 待办事项
     *
     * @return
     */
    public List<UserCorrelation> findBacklog() {
        return projectsItemRepository.findBacklog();
    }

    public Project1 findProject1ById(Long id) {
        return projectsItemRepository.findProject1ById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateProject1Status(Long status, Long id) {
        projectsItemRepository.updateProject1Status(status, id);
    }

    public Project1 findProjectsById(Long id) {
        Optional<Project1> p=projectsItemRepository.findById(id);
        return p.get();
    }

    public List<UserCorrelation> findTaskAllById(List<Long> id){
        return taskocationRepository.findAllByIdIsUser(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveProject1(Long id) {
        projectsItemRepository.updateProject1Status((long)2,id);
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveProject2(Long id) {
        projectsItemRepository.updateProject1Status((long)0,id);
    }
}
